package com.kcht.itsed.mws.devicescheduler.biz;

import com.shhic.itsed.mws.deviceintf.IReaderResultCallback;
import com.shhic.itsed.mws.deviceintf.ReaderCommand;
import com.kcht.itsed.mws.devicescheduler.facade.IPhysicalReaderDriverRegister;
import com.kcht.itsed.mws.devicescheduler.facade.IReaderCommandNotifier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Set;

@Slf4j
@Component
public class ReaderCommandNotifier implements IReaderCommandNotifier{

	
	
	@Autowired
	IPhysicalReaderDriverRegister driverRegister;
	
	@Autowired
	@Qualifier("MWSReaderOperateRsultCallback")
	IReaderResultCallback readerResultCallback;
	
	@Override
	@Async
	public void notifyCycleReaderCommand(Set<ReaderCommand> readerCommandSet) {
		//处理阅读器指令通知-循环
		readerCommandSet.forEach(rc->{
			driverRegister.fetchDriver(rc.getReaderClass()).ifPresent(driver->{
				log.info("--------------- begin dispatching command to reader:" + rc.getReaderIp() + "----------------");
				driver.dispatchReadCommand(rc.getReaderIp(),rc,-1,readerResultCallback);
				log.info("--------------- finish dispatching command to reader:" + rc.getReaderIp() + "----------------");
			});
		});
		log.info("READER COMMAND SET size:{}",readerCommandSet.size());
		log.info("READER COMMAND SET:{}", readerCommandSet);
	}
	
	
	@Override
	public void notifySingleReaderCommand(Set<ReaderCommand> readerCommandSet) {
		// TODO 处理阅读器指令通知-单次
		
	}
}
